package com.guli.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.handler.EduException;
import com.guli.common.result.ResultCodeEnum;
import com.guli.edu.entity.EduCourse;
import com.guli.edu.entity.EduTeacher;

import com.guli.edu.service.EduCourseService;
import com.guli.edu.service.EduTeacherService;
import com.guli.edu.vo.QueryTeacher;
import com.guli.common.result.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Daniel
 * @since 2019-03-19
 */

@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Autowired
    private EduCourseService eduCourseService  ;

    @ApiOperation(value = "讲师列表用户页面使用")
    @GetMapping("{page}/{limit}")
    public R testList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<EduTeacher>  pageTeacher = new Page<>(page,limit);
        Map<String,Object> map  = eduTeacherService.pageTeacherList(pageTeacher);
        return R.ok().data(map) ;
    }

    @ApiOperation(value = "讲师列表")
    @GetMapping("/list")
    public R getList() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("list", list);
    }


    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
//        boolean flag = eduTeacherService.removeById(id);
        boolean flag = eduTeacherService.deleteById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


//    @ApiOperation(value = "分页讲师列表")
//    @GetMapping("{page}/{limit}")
//    public R pageList(
//            @ApiParam(name = "page", value = "当前页码", required = true)
//            @PathVariable Long page,
//            @ApiParam(name = "limit", value = "每页记录数", required = true)
//            @PathVariable Long limit
//    ) {
//        if (page <= 0 || limit <= 0) {
//            //throw new GuliException(21003, "参数不正确1");
//            throw new EduException(ResultCodeEnum.PARAM_ERROR);
//        }
//        //模拟异常 为了测试 以后删除的
//        try {
//            int a = 9 / 0;
//        } catch (Exception e) {
//            throw new EduException(20000, "执行自定义异常");
//        }
//        Page<EduTeacher> pageParam = new Page<>(page, limit);
//        eduTeacherService.page(pageParam, null);
//        List<EduTeacher> records = pageParam.getRecords();
//        Long total = pageParam.getTotal();
//
//        return R.ok().data("total", total).data("rows", records);
//
//    }

    /***
     * requestbody json 字符串
     {
     "begin": "2017-01-12 00:00:00",
     "end": "2019-03-30 00:00:00",
     "level": 2,
     "name": "周润发"
     }
     */
    @ApiOperation(value = "根据条件分页讲师列表")
    @PostMapping("pageList/{page}/{limit}")
    public R pageListCondition(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit, @RequestBody QueryTeacher queryTeacher
    ) {

        if (page <= 0 || limit <= 0) {
            //throw new GuliException(21003, "参数不正确1");
            throw new EduException(ResultCodeEnum.PARAM_ERROR);
        }

        Page<EduTeacher> pageParam = new Page<>(page, limit);
        eduTeacherService.pageList(pageParam, queryTeacher);
        List<EduTeacher> records = pageParam.getRecords();
        Long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);

    }

    //5 添加讲师
    @ApiOperation(value = "添加讲师")
    @PutMapping
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //6.根据ID查询
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(@PathVariable String id) {
        //查询讲师信息
        EduTeacher teacher = eduTeacherService.getById(id);
        //根据讲师id查询这个讲师的课程列表
        List<EduCourse> courseList = eduCourseService.selectByTeacherId(id);

        return R.ok().data("eduTeacher", teacher).data("courseList",courseList);
    }

    //7.修改
    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher/{id}")
    public R updateTeacher(@PathVariable String id, @RequestBody EduTeacher eduTeacher) {
        eduTeacher.setId(id);
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 登录
    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login() {
        //{"code":20000,"data":{"token":"admin"}}
        return R.ok().data("token", "admin");
    }

    //信息
    // 登录
    @ApiOperation(value = "登录信息")
    @GetMapping("info")
    public R info() {
        //{"code":20000,"data":{"roles":["admin"],"name":"admin","avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"}}
        return R.ok().data("roles", "[admin]").data("name", "admin").data("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }
}

